package com.chengwang.bookstory.service.Imple;

import com.chengwang.bookstory.mapper.UserMapper;
import com.chengwang.bookstory.pojo.User;
import com.chengwang.bookstory.service.MailService;
import com.chengwang.bookstory.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImple implements UserService {
    @Autowired
    MailService mailService;
    @Autowired
    private UserMapper userMapper;
    @Override
    public Boolean uRegist(User user) {
        String userAccount=user.getUserAccount();
        List<User> list = userMapper.queryUser();
        for (int i = 0; i <list.size() ; i++) {
            String userAccount1 = list.get(i).getUserAccount();
            if(userAccount1==userAccount){
                return false;
            }
        }
        int i = userMapper.addUser(user);
        String message="图书商城网欢迎你，<a href='http://localhost:8080/user/active?userAccount="+user.getUserAccount()+"'>请激活邮箱</a>";
        if(i==1){
            mailService.sendSimpleMail(user.getUserMail(),"您注册商城需要激活",message);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public User uLogin(User user) {
        User user1 = userMapper.uLogin(user);

        return user1;
    }

    @Override
    public List<User> queryUser() {
        List<User>list=userMapper.queryUser();
        return list;
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public User queryUserByUserAccount(String userAccount) {
        return userMapper.queryUserByUserAccount(userAccount);
    }

    @Override
    public void updateStatus(String userAccount) {
        userMapper.toOff(userAccount);
    }

    @Override
    public void toOpen(String userAccount) {

        userMapper.toOpen(userAccount);
    }

    @Override
    public void active(String userAccount) {
        userMapper.active(userAccount);
    }
}
